import pymysql
class DBUtil:
    @classmethod
    def __get_conn(cls):
        conn = pymysql.connect(host='211.103.136.244',port=7061,user='student'
                               ,password='iHRM_student_2021',database='ihrm',charset='utf8')
        return conn
    @classmethod
    # 查询
    def select_one(cls,sql):
        cursor=None
        conn=None
        res = None
        try:
            conn = cls.__get_conn()
            cursor = conn.cursor()
            cursor.execute(sql)
            res = cursor.fetchone()
        except Exception as e:
            print(e)
        finally:
            cursor.close()
            conn.close()
            return res
    @classmethod
        # 查询多条
    def select_many(cls,sql,a):
        cursor=None
        conn=None
        res = None
        try:
            conn = cls.__get_conn()
            cursor = conn.cursor()
            cursor.execute(sql)
            res = cursor.fetchmany(a)
        except Exception as e:
            print(e)
        finally:
            cursor.close()
            conn.close()
            return res
    @classmethod
        # 查询所有
    def select_chall(cls,sql):
        cursor=None
        conn=None
        res = None
        try:
            conn = cls.__get_conn()
            cursor = conn.cursor()
            cursor.execute(sql)
            res = cursor.fetchall()
        except Exception as e:
            print(e)
        finally:
            cursor.close()
            conn.close()
            return res
    @classmethod
        # 增删改
    def uid_db(cls,sql):
        cursor=None
        conn=None
        res = None
        try:
            conn = cls.__get_conn()
            cursor = conn.cursor()
            cursor.execute(sql)
            conn.commit()
            print('提交事务')
            print('影响行数',conn.affected_rows())
        except Exception as e:
            print(e)
            conn.rollback()
            print('回滚事务')
        finally:
            cursor.close()
            conn.close()
if __name__ == '__main__':
    res=DBUtil.select_one('select * from bs_user where mobile=13905278546')
    print(res)
    res2=DBUtil.uid_db('insert into bs_user(id,mobile,username,password) values(999999999999,13419876280,"tom",123456)')
    print(res2)
    res4 = DBUtil.select_one('select * from bs_user where id=id=999999999999')
    print(res4)
    res4 = DBUtil.uid_db('delete from bs_user where id=999999999999')
    print(res4)